/**
 * 初始化 BootStrap Table 的封装
 *
 * 约定：toolbar的id为 (bstableId + "Toolbar")
 *
 * @author fengshuonan
 */
(function () {
    var BTable = function (bstableId, columns, options) {
        this.btInstance = null; //jquery和BootStrapTable绑定的对象
        this.bstableId = bstableId;
        this.toolbarId = '#' + bstableId + "Toolbar";
        this.url = "";
        this.fullTextUrl = "";
        this.addUrl = "";
        this.addChildUrl="";
        this.editUrl = "";
        this.delUrl = "";
        this.unDelUrl = "";
        this.profixTitle = "";
        this.columns = columns;
        this.idField = "id";
        this.options = options;
//        this.data = {"fdc": "init"};
//        this.queryParams = function (params) {
//            return params;
//        };
    };
    BTable.prototype = {
        /**
         * 初始化bootstrap table
         *  */
        init: function (funProps) {
            this.url = funProps.url;
            this.profixTitle = funProps.profixTitle;
            this.fullTextUrl = funProps.fullTextUrl;
            this.addUrl = funProps.addUrl;
            this.addChildUrl = funProps.addChildUrl;
            this.editUrl = funProps.editUrl;
            this.delUrl = funProps.delUrl;
            this.unDelUrl = funProps.unDelUrl;
            var defaults = {
                height: 665,
                classes: 'table table-bordered table-hover table-striped table-sm',
                theadClasses: 'thead-light',
                silentSort: false,
                columns: this.columns,
                url: this.url,
                method: 'post',
                contentType: "application/x-www-form-urlencoded",
                cache: 'false',
//                ajaxOptions: {data: this.data}, //ajax请求的附带参数
                queryParams: this.queryParams,
                pagination: true,
                sidePagination: 'server',
                pageSize: 20, //每页的记录行数（*）
                pageList: [2, 5, 10, 20, 30, 40, 50, 100],
                paginationPreText: 'Previous',
                paginationNextText: 'Next',
                paginationSuccessivelySize: 10,
                paginationPagesBySide: 3,
                paginationUseIntermediate: true,
                showColumns: true,
                minimumCountColumns: 2,
                showPaginationSwitch: false,
                showRefresh: true,
                showToggle: true,
                showFullscreen: true,
                escape: true,
                idField: this.idField,
                selectItemName: this.idField,
                clickToSelect: true,
                maintainSelected: true,
                toolbar: this.toolbarId,
                buttonsClass: 'outline-primary'//primary secondary success danger warning info light dark / outline-warning
            };
            this.options = $.extend(defaults, this.options);
            this.btInstance = $('#' + this.bstableId).bootstrapTable(this.options);
            return this;
        },
        //自定义查询参数回调后，执行查询
        search: function (formId) {
            this.refresh({url: this.url, pageNumber: 1});
        },
        // 自定义查询参数回调后，执行全文查询
        fullTextSearch: function () {
            this.refresh({url: this.fullTextUrl, pageNumber: 1});
        },
        // 刷新表格
        refresh: function (parms) {
//            var params = this.btInstance.bootstrapTable('getOptions');
            if (typeof parms != "undefined") {
                this.btInstance.bootstrapTable('refresh', parms);
            } else {
                this.btInstance.bootstrapTable('refresh');
            }
        },
        add: function (width, height) {
            var title = this.profixTitle + "添加";
            var url = this.addUrl;
            if (width && height) {

                $.popup.open(title, url, width, height);
            } else
                $.popup.openDefault(title, url);
        }, addChild: function (id, width, height) {
            var title = this.profixTitle + "添加";
            var url = this.addChildUrl;
            var row = new Array();
            if (id)
                row.push(id);
           
            if ($.common.isEmpty(row) ) {
                $.alertMsg.alertWarning("没有父记录标识");
                return;
            } else {
                url = url + row[0];
            }
            if (width && height) {

                $.popup.open(title, url, width, height);
            } else
                $.popup.openDefault(title, url);
        },edit: function (id, width, height) {
            var title = this.profixTitle + "修改";
            var url = this.editUrl;
            var row = new Array();
            if (id)
                row.push(id);
            else
                row = this.getSelectionIds();
            if ($.common.isEmpty(row) || row.length > 1) {
                $.alertMsg.alertWarning("请选择一条记录");
                return;
            } else {
                url = url + row[0];
            }
            if (width && height) {

                $.popup.open(title, url, width, height);
            } else
                $.popup.openDefault(title, url);
        }, undelete: function (id) {
            var url = this.unDelUrl;
            var row = new Array();
            if (id)
                row.push(id);
            else
                row = this.getSelectionIds();
            if ($.common.isEmpty(row) || row.length == 0) {
                $.alertMsg.alertWarning("请至少选择一条记录");
                return;
            }
            layer.confirm('确定恢复所选择的已删除记录吗?', {
                lbtable: this,
                yes: function (index, layero) {
                    var lyins = this;
                    var config = {
                        url: url,
                        type: "post",
                        dataType: "json",
                        data: {'id': row.toString()},
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $.alertMsg.msgSuccess(result.msg);
                                lyins.lbtable.refresh();
                            } else if (result.code == web_status.FAIL) {
                                $.alertMsg.alertError(result.msg);
                            } else {
                                $.alertMsg.alertWarning(result.msg);
                            }
                        }
                    };
                    $.ajax(config);
                }
            });

        },
        delete: function (id) {
            var url = this.delUrl;
            var row = new Array();
            if (id)
                row.push(id);
            else
                row = this.getSelectionIds();
            if ($.common.isEmpty(row) || row.length == 0) {
                $.alertMsg.alertWarning("请至少选择一条记录");
                return;
            }
            layer.confirm('确定删除选择的记录吗?', {
                lbtable: this,
                yes: function (index, layero) {
                    var lyins = this;
                    var config = {
                        url: url,
                        type: "post",
                        dataType: "json",
                        data: {'ids': row.toString()},
                        success: function (result) {
                            if (result.code == web_status.SUCCESS) {
                                $.alertMsg.msgSuccess(result.msg);
                                lyins.lbtable.refresh();
                            } else if (result.code == web_status.FAIL) {
                                $.alertMsg.alertError(result.msg);
                            } else {
                                $.alertMsg.alertWarning(result.msg);
                            }
                        }
                    };
                    $.ajax(config);
                }
            });
        },
        //获取当前表格选择的id，多个
        getSelectionIds: function () {
            var row = $("#" + this.bstableId).bootstrapTable('getSelections');
            var arr = new Array();
            for (var i = 0; i < row.length; i++) {
                arr.push(row[i][this.idField])
            }
            return arr;
        }
    }
    ;
    window.BTable = BTable;
}());